

import rewardData from './rewardData'

const {a,b} = rewardData
// console.log("a",aaa)

const rewardA = a.rewardList
const rewardB = b.rewardList
const rewardAProby = 75
const rewardBProby = 25

const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('/') 
}
const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

export default {

    // 摇奖抽取奖品
    draw(){

        const probability = parseInt(Math.random()*100)
        let reward ;

        // 概率A
        if(probability<=rewardBProby){
            const rewardIndex = parseInt(Math.random()*10)%(rewardB.length)

            console.log("probability",probability,rewardIndex,rewardB)
            reward =  rewardB[rewardIndex]
        }
        // 概率B
        else {
            const rewardIndex = parseInt(Math.random()*10)%(rewardA.length)

            console.log("probability",probability,rewardIndex,rewardA)
            reward = rewardA[rewardIndex]
        }

        return {
            ...reward,
            time:formatTime(new Date())
        }

    },

    // 获取摇奖次数
    getDrawNums(){
        const drawNums = +localStorage.getItem("drawNums")
        return drawNums||0
    },

    // 设置摇奖次数
    setDrawNums(num){
        const drawNums = +localStorage.getItem("drawNums")||0

        // 最低有0次
        if(num<0 && drawNums===0) return drawNums

        // 最多只有6次
        if(num>0 && drawNums>=6) return drawNums

        localStorage.setItem("drawNums",drawNums+num)

    },

    // 获取奖品信息
    get(){
        const rewardList = JSON.parse(localStorage.getItem("rewardList"))||[]

        return rewardList
    },

    // 保存奖品信息
    save(reward){
        const rewardList = JSON.parse(localStorage.getItem("rewardList"))||[]

        localStorage.setItem("rewardList",JSON.stringify([...rewardList,reward]))
    }
}